package pi.client.gui;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;

import javax.swing.JButton;

import pi.client.core.MyGame;
import pi.client.units.UnitBase;
import pi.client.utils.ImageUtils;

@SuppressWarnings("serial")
public class MyPanelPreGame extends MyPanelBase{
	private JButton connectButton;
	private ActionListener connectButtonEvent;
	
	private JButton clearTeamButton;
	private ActionListener clearTeamButtonEvent;
	
	private Rectangle rectBowSelection;
	private Rectangle rectCrownSelection;
	private Rectangle rectSwordSelection;
	
	private BufferedImage imageBowSelection;
	private BufferedImage imageCrownSelection;
	private BufferedImage imageSwordSelection;
	
	private boolean unit1Selected;
	private boolean unit2Selected;
	private boolean unit3Selected;
	private boolean unit4Selected;
	private boolean unit5Selected;
	
	public int idUnit1;
	public int idUnit2;
	public int idUnit3;
	public int idUnit4;
	public int idUnit5;
	
	private BufferedImage unit1Image;
	private BufferedImage unit2Image;
	private BufferedImage unit3Image;
	BufferedImage unit4Image;
	BufferedImage unit5Image;	
	
	private int currentSelection;
	
	private boolean kingSelected;
	
	public MyPanelPreGame(final MyMainPanel mainPanel, String backgroundFilename, int width, int height){
		super(mainPanel, backgroundFilename, width, height);
		setLayout(null);
		
		int connectButtonWidth = 100;
		connectButton = new JButton("Connect");
		connectButton.setBounds(width/2 - connectButtonWidth/2, height/2 + 200, connectButtonWidth, 30);
		connectButton.setEnabled(false);
		
		connectButtonEvent = new ActionListener(){			
			@Override public void actionPerformed(ActionEvent event){
				TryToConnectOnServer();
			}
		};
		connectButton.addActionListener(connectButtonEvent);

		add(connectButton);
		
		clearTeamButton = new JButton("Erase Team");
		clearTeamButton.setBounds(width-150, 30, 120, 30);
		
		clearTeamButtonEvent = new ActionListener(){			
			@Override public void actionPerformed(ActionEvent event){
				EraseTeamSelection();
			}
		};
		clearTeamButton.addActionListener(clearTeamButtonEvent);

		add(clearTeamButton);
		
		int unitsSizeButton = 100;
		rectBowSelection = new Rectangle(width/2 - unitsSizeButton*2, height/2, 100, 100);
		rectCrownSelection = new Rectangle(width/2 - unitsSizeButton/2, height/2 , 100, 100);
		rectSwordSelection = new Rectangle(width/2 + unitsSizeButton, height/2 , 100, 100);

		imageBowSelection = ImageUtils.LoadImage("res/images/units/logos/Bow.png");
		imageCrownSelection = ImageUtils.LoadImage("res/images/units/logos/Crown.png");
		imageSwordSelection = ImageUtils.LoadImage("res/images/units/logos/Sword.png");

		unit1Selected = false;
		unit1Selected = false;
		unit1Selected = false;
		unit1Selected = false;
		unit1Selected = false;
		
		idUnit1 = 0;
		idUnit2 = 0;
		idUnit3 = 0;
		idUnit4 = 0;
		idUnit5 = 0;
		
		currentSelection = 1;
		
		kingSelected = false;
	}
	
	private void EraseTeamSelection(){		
		unit1Selected = false;
		unit2Selected = false;
		unit3Selected = false;
		unit4Selected = false;
		unit5Selected = false;
		
		idUnit1 = 0;
		idUnit2 = 0;
		idUnit3 = 0;
		idUnit4 = 0;
		idUnit5 = 0;
		
		currentSelection = 1;
		
		kingSelected = false;
		
		repaint();
	}
	
	private void TryToConnectOnServer(){
//		mainPanel.ResetPlayers();
		
		if(mainPanel.connection.connected == false){
			if(mainPanel.connection.ConnectToServer("localhost", 34523)){
				mainPanel.SetCurrentPanel(mainPanel.gamePanel);
				
				mainPanel.connection.StartNetworking();
				
				mainPanel.playerMe.CreateUnits(idUnit1, 0, 1, idUnit2, 0, 2, idUnit3, 0, 3, idUnit4, 0, 4, idUnit5, 0, 5);				
				
				mainPanel.connection.RegisterName();
				mainPanel.connection.RegisterMyUnits();
				mainPanel.connection.RequestServerSize();
				mainPanel.connection.RequestServerMapType();
				
				mainPanel.program.game = new MyGame(mainPanel.program);
				mainPanel.program.game.StartGame();
			}
		}else{
			mainPanel.connection.SetConnection(false);
			
//			mainPanel.SetCurrentPanel(mainPanel.gamePanel);
//			mainPanel.program.game = new MyGame(mainPanel.program);
//			mainPanel.program.game.StartGame();
		}
	}
	
	@Override protected void Draw(Graphics g){
		super.Draw(g);
		
		DrawTitle(g, "Make Your Team!", width/2, 100);
		
		g.drawImage(imageBowSelection, rectBowSelection.x, rectBowSelection.y, rectBowSelection.width, rectBowSelection.height, null);
		g.drawImage(imageCrownSelection, rectCrownSelection.x, rectCrownSelection.y, rectCrownSelection.width, rectCrownSelection.height, null);
		g.drawImage(imageSwordSelection, rectSwordSelection.x, rectSwordSelection.y, rectSwordSelection.width, rectSwordSelection.height, null);
		
		int yOfUnitsButton = rectBowSelection.y - 150;
		int xOfUnitsButton = 100;
		int widthButton = 100;
		int heightButton = 100;
		
		//space selection 1
		if(unit1Selected == true){
			g.drawImage(unit1Image, xOfUnitsButton - widthButton/2, yOfUnitsButton, widthButton, heightButton, null);
			g.setColor(Color.green);	
		}else{
			g.setColor(Color.white);
		}
		g.drawRect(xOfUnitsButton - widthButton/2, yOfUnitsButton, widthButton, heightButton);

		//space selection 2		
		if(unit2Selected == true){
			g.drawImage(unit2Image, xOfUnitsButton*2, yOfUnitsButton, widthButton, heightButton, null);
			g.setColor(Color.green);	
		}else{
			g.setColor(Color.white);
		}
		g.drawRect(xOfUnitsButton*2, yOfUnitsButton, widthButton, heightButton);

		//space selection 3
		if(unit3Selected == true){
			g.drawImage(unit3Image, xOfUnitsButton*3 + widthButton/2, yOfUnitsButton, widthButton, heightButton, null);
			g.setColor(Color.green);	
		}else{
			g.setColor(Color.white);
		}
		g.drawRect(xOfUnitsButton*3 + widthButton/2, yOfUnitsButton, widthButton, heightButton);
		
		//space selection 4
		if(unit4Selected == true){
			g.drawImage(unit4Image, xOfUnitsButton*4 + widthButton, yOfUnitsButton, widthButton, heightButton, null);
			g.setColor(Color.green);	
		}else{
			g.setColor(Color.white);
		}
		g.drawRect(xOfUnitsButton*4 + widthButton, yOfUnitsButton, widthButton, heightButton);
		
		//space selection 5
		if(unit5Selected == true){
			g.drawImage(unit5Image, xOfUnitsButton*5 + (widthButton/2)*3, yOfUnitsButton, widthButton, heightButton, null);
			g.setColor(Color.green);	
		}else{
			g.setColor(Color.white);
		}
		g.drawRect(xOfUnitsButton*5 + (widthButton/2)*3, yOfUnitsButton, widthButton, heightButton);
	}
	
	@Override protected void MouseClickedEvent(MouseEvent e){
		if(currentSelection < 5 || kingSelected == false){
			connectButton.setEnabled(false);
		}else{
			connectButton.setEnabled(true);
		}
			
		if(rectBowSelection.contains(e.getX(), e.getY()) == true){
			switch(currentSelection){
				case 1:
					unit1Selected = true;
					idUnit1 = UnitBase.UnitTypeSoldierArcher;
					unit1Image = imageBowSelection;
				break;				
				case 2:
					unit2Selected = true;
					idUnit2 = UnitBase.UnitTypeSoldierArcher;
					unit2Image = imageBowSelection;
				break;				
				case 3:
					unit3Selected = true;
					idUnit3 = UnitBase.UnitTypeSoldierArcher;
					unit3Image = imageBowSelection;
				break;				
				case 4:
					unit4Selected = true;
					idUnit4 = UnitBase.UnitTypeSoldierArcher;
					unit4Image = imageBowSelection;
				break;				
				case 5:
					unit5Selected = true;
					idUnit5 = UnitBase.UnitTypeSoldierArcher;
					unit5Image = imageBowSelection;
				break;
			}

			currentSelection++;
			repaint();
		}
		
		if(kingSelected == false){
			if(rectCrownSelection.contains(e.getX(), e.getY()) == true){
				switch(currentSelection){
					case 1:
						unit1Selected = true;
						idUnit1 = UnitBase.UnitTypeKing;
						unit1Image = imageCrownSelection;
					break;				
					case 2:
						unit2Selected = true;
						idUnit2 = UnitBase.UnitTypeKing;
						unit2Image = imageCrownSelection;
					break;				
					case 3:
						unit3Selected = true;
						idUnit3 = UnitBase.UnitTypeKing;
						unit3Image = imageCrownSelection;
					break;				
					case 4:
						unit4Selected = true;
						idUnit4 = UnitBase.UnitTypeKing;
						unit4Image = imageCrownSelection;
					break;				
					case 5:
						unit5Selected = true;
						idUnit5 = UnitBase.UnitTypeKing;
						unit5Image = imageCrownSelection;
					break;
				}
		
				kingSelected = true;
				currentSelection++;
				repaint();
			}
		}
		
		if(rectSwordSelection.contains(e.getX(), e.getY()) == true){
			switch(currentSelection){
				case 1:
					unit1Selected = true;
					idUnit1 = UnitBase.UnitTypeSoldier;
					unit1Image = imageSwordSelection;
				break;				
				case 2:
					unit2Selected = true;
					idUnit2 = UnitBase.UnitTypeSoldier;
					unit2Image = imageSwordSelection;
				break;				
				case 3:
					unit3Selected = true;
					idUnit3 = UnitBase.UnitTypeSoldier;
					unit3Image = imageSwordSelection;
				break;				
				case 4:
					unit4Selected = true;
					idUnit4 = UnitBase.UnitTypeSoldier;
					unit4Image = imageSwordSelection;
				break;				
				case 5:
					unit5Selected = true;
					idUnit5 = UnitBase.UnitTypeSoldier;
					unit5Image = imageSwordSelection;
				break;
			}
	
			currentSelection++;
			repaint();
		}
	}
}